#include "trm/target_frame.h"
#include <imglib/downsample.h>

namespace trm {

target_frame::target_frame(segment & segm) : 
	_segm(segm),
	_time(~0)
{ 
}

target_frame::~target_frame()
{
}

void target_frame::create(unsigned time)
{ 
	direction dir;
	unsigned offset = _segm.segtime(time, &dir);
	create(dir, offset); 
}

void target_frame::create(direction dir, unsigned offset)
{
	unsigned time = _segm.time(dir, offset);
	if (_time == time)
		return;
	_time = time;
	_image = _segm.load_frame(_time, dir);

	if (_segm.param().downsample > 1) {
		mswin::timing_mark mark(timing_mark("image downsampling"));
		img::downsample(_image->im, _downsampled_image, _segm.param().downsample);
	} else
		_downsampled_image.clear();

	if (_segm.param().optic_flow)
		_float_image = downsampled_image();
	else
		_float_image.clear();
}

void target_frame::swap(target_frame & t)
{
	assert(&_segm == &t._segm);
	std::swap(_time, t._time);
	_image.swap(t._image);
	_downsampled_image.swap(t._downsampled_image);
	_float_image.swap(t._float_image);
}

} //namespace trm
